<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class VipController extends Controller
{
    //列表显示
    public function getIndex(Request $request)
    {
    	//读取数据库
    	$vips = DB::table('user')
    		->where('dengji','>','0')
    		->where(function($query) use($request){
    			$k = $request->input('keyword');
	    		if(!empty($k)){
	    			$query->where('username','like','%'.$k.'%');
	    		}
    	})->paginate(3);
    	// dd($vips->all());	$request->input('num',5)
    	return view('admin.vip.index',['vip'=>$vips,'request'=>$request]);
    }

    //会员修改页
    public function getEdit(Request $request)
    {
    	//获取当前用户的id
    	$id = $request->input('id');
    	//读取数据库中该用户的信息
    	$vipuser = DB::table('user')->where('id',$id)->first();
    	// dd($vipuser);
    	return view('admin.vip.edit',['vipuser'=>$vipuser]);
    }

    //插入
    public function postUpdate(Request $request)
    {
    	// dd($request->all());
    	$data = $request->except(['_token']);
    	// dd($data);
    	//更新数据库
    	$res = DB::table('user')->where('id',$request->input('id'))->update($data);
    	// dd($res);
        if($res){
            return redirect('/vip/index')->with('info','修改成功');
        }else{
            return back()->with('error','修改失败,请重新修改!');
        }
    }

    //删除
    public function getDelete(Request $request)
    {
    	$id = $request->input('id');
    	$res = DB::table('user')->where('id',$id)->delete();
    	if($res){
    		return back()->with('info','删除成功');
    	}else{
    		return back()->with('error','删除失败');
    	}
    }

    /**
     * 订单管理
     */
    public function getOrder(Request $request)
    {
        return view('admin.vip.order');
    }

    public function detail()
    {
        //会员详情页面
        return view('home.vip.detail');
    }

    public function order(Request $request)
    {
        //会员下单页面
        return view('home.vip.order');  
    }

    public function pay(Request $request)
    {
        // dd($request->all());
        $price = $request->input('price');
        $payment = $request->input('payment');
        //根据id读取商品的信息
        $goods = DB::table('goods')->where('price',$price)->first();
        // dd($goods);
        //支付页面
        return view('home.vip.pay',['goods'=>$goods,'payment'=>$payment]);
    }

    public function finish(Request $request)
    {
        $data = $request->except(['_token']);
        //将数据插入到数据库
        $res = DB::table('order')->insert($data);
        //判断
        if($res){
            //return             
        }else{
            return back()->with('error','订单失效');
        }
    }
}
